﻿//------------------------------------------------------------------------------
// 创建标识: Copyright (C) 2009 Socansoft.com 版权所有
// 创建描述: SocanCode代码生成器自动创建于 2009-5-14 15:23:39
//
// 功能描述: 
//
// 修改标识: 
// 修改描述: 
//------------------------------------------------------------------------------

using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using IDAL.TaoBao;
using DBUtility;
using System.Collections;
using System.Collections.Generic;

namespace SQLServerDAL.TaoBao
{
    /// <summary>
    /// 数据访问类 TB_XSJRD_JY
    /// </summary>
    public class TB_XSJRD_JY : ITB_XSJRD_JY
    {
        public TB_XSJRD_JY()
        { }

        #region  成员方法
        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(Model.TaoBao.TB_XSJRD_JY model)
        {
            int rowsAffected;
            SqlParameter[] parameters = {
                    new SqlParameter("@MID", SqlDbType.Int),
                    new SqlParameter("@PID", SqlDbType.Int),
                    new SqlParameter("@TN_ID", SqlDbType.Int),
                    new SqlParameter("@alipay_no", SqlDbType.VarChar,50),
                    new SqlParameter("@payment", SqlDbType.VarChar,50),
                    new SqlParameter("@status", SqlDbType.VarChar,50),
                    new SqlParameter("@type", SqlDbType.VarChar,50),
                    new SqlParameter("@created", SqlDbType.DateTime),
                    new SqlParameter("@seller_rate", SqlDbType.Int),
                    new SqlParameter("@buyer_rate", SqlDbType.Int),
                    new SqlParameter("@seller_rate_content", SqlDbType.Text),
                    new SqlParameter("@buyer_rate_content", SqlDbType.Text),
                    new SqlParameter("@field1", SqlDbType.VarChar,255),
                    new SqlParameter("@field2", SqlDbType.VarChar,255)
            };
            parameters[0].Direction = ParameterDirection.Output;

            parameters[0].Value = model.MID;
            parameters[1].Value = model.PID;
            parameters[2].Value = model.TN_ID;

            if (model.alipay_no != null)
                parameters[3].Value = model.alipay_no;
            else
                parameters[3].Value = DBNull.Value;


            if (model.payment != null)
                parameters[4].Value = model.payment;
            else
                parameters[4].Value = DBNull.Value;


            if (model.status != null)
                parameters[5].Value = model.status;
            else
                parameters[5].Value = DBNull.Value;


            if (model.type != null)
                parameters[6].Value = model.type;
            else
                parameters[6].Value = DBNull.Value;


            if (model.created != DateTime.MinValue)
                parameters[7].Value = model.created;
            else
                parameters[7].Value = DBNull.Value;

            parameters[8].Value = model.seller_rate;
            parameters[9].Value = model.buyer_rate;

            if (model.seller_rate_content != null)
                parameters[10].Value = model.seller_rate_content;
            else
                parameters[10].Value = DBNull.Value;


            if (model.buyer_rate_content != null)
                parameters[11].Value = model.buyer_rate_content;
            else
                parameters[11].Value = DBNull.Value;


            if (model.field1 != null)
                parameters[12].Value = model.field1;
            else
                parameters[12].Value = DBNull.Value;


            if (model.field2 != null)
                parameters[13].Value = model.field2;
            else
                parameters[13].Value = DBNull.Value;

            SqlHelper.RunProcedure(SqlHelper.LocalSqlServer, "sp_TB_XSJRD_JY_ADD", parameters, out rowsAffected);

            if (rowsAffected > 0)
                return (int)parameters[0].Value;
            else
                return 0;
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Model.TaoBao.TB_XSJRD_JY model)
        {
            int rowsAffected;
            SqlParameter[] parameters = {
                    new SqlParameter("@MID", SqlDbType.Int),
                    new SqlParameter("@PID", SqlDbType.Int),
                    new SqlParameter("@TN_ID", SqlDbType.Int),
                    new SqlParameter("@alipay_no", SqlDbType.VarChar,50),
                    new SqlParameter("@payment", SqlDbType.VarChar,50),
                    new SqlParameter("@status", SqlDbType.VarChar,50),
                    new SqlParameter("@type", SqlDbType.VarChar,50),
                    new SqlParameter("@created", SqlDbType.DateTime),
                    new SqlParameter("@seller_rate", SqlDbType.Int),
                    new SqlParameter("@buyer_rate", SqlDbType.Int),
                    new SqlParameter("@seller_rate_content", SqlDbType.Text),
                    new SqlParameter("@buyer_rate_content", SqlDbType.Text),
                    new SqlParameter("@field1", SqlDbType.VarChar,255),
                    new SqlParameter("@field2", SqlDbType.VarChar,255)
            };
            parameters[0].Value = model.MID;
            parameters[1].Value = model.PID;
            parameters[2].Value = model.TN_ID;

            if (model.alipay_no != null)
                parameters[3].Value = model.alipay_no;
            else
                parameters[3].Value = DBNull.Value;


            if (model.payment != null)
                parameters[4].Value = model.payment;
            else
                parameters[4].Value = DBNull.Value;


            if (model.status != null)
                parameters[5].Value = model.status;
            else
                parameters[5].Value = DBNull.Value;


            if (model.type != null)
                parameters[6].Value = model.type;
            else
                parameters[6].Value = DBNull.Value;


            if (model.created != DateTime.MinValue)
                parameters[7].Value = model.created;
            else
                parameters[7].Value = DBNull.Value;

            parameters[8].Value = model.seller_rate;
            parameters[9].Value = model.buyer_rate;

            if (model.seller_rate_content != null)
                parameters[10].Value = model.seller_rate_content;
            else
                parameters[10].Value = DBNull.Value;


            if (model.buyer_rate_content != null)
                parameters[11].Value = model.buyer_rate_content;
            else
                parameters[11].Value = DBNull.Value;


            if (model.field1 != null)
                parameters[12].Value = model.field1;
            else
                parameters[12].Value = DBNull.Value;


            if (model.field2 != null)
                parameters[13].Value = model.field2;
            else
                parameters[13].Value = DBNull.Value;


            SqlHelper.RunProcedure(SqlHelper.LocalSqlServer, "sp_TB_XSJRD_JY_Update", parameters, out rowsAffected);
            return rowsAffected > 0;
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int MID)
        {
            int rowsAffected;
            SqlParameter[] parameters = {
                    new SqlParameter("@MID", SqlDbType.Int)};
            parameters[0].Value = MID;

            SqlHelper.RunProcedure(SqlHelper.LocalSqlServer, "sp_TB_XSJRD_JY_Delete", parameters, out rowsAffected);
            return rowsAffected > 0;
        }

        /// <summary>
        /// 得到最大ID
        /// </summary>
        public int GetMaxId()
        {
            int rowsAffected;
            SqlParameter[] parameters ={ };
            return SqlHelper.RunProcedure(SqlHelper.LocalSqlServer, "sp_TB_XSJRD_JY_GetMaxId", parameters, out rowsAffected);
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int MID)
        {
            int rowsAffected;
            SqlParameter[] parameters = {
                    new SqlParameter("@MID", SqlDbType.Int)};
            parameters[0].Value = MID;
            return SqlHelper.RunProcedure(SqlHelper.LocalSqlServer, "sp_TB_XSJRD_JY_Exists", parameters, out rowsAffected) == 1;
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Model.TaoBao.TB_XSJRD_JY GetModel(int MID)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@MID", SqlDbType.Int)};
            parameters[0].Value = MID;
            DataSet ds = SqlHelper.RunProcedure(SqlHelper.LocalSqlServer, "sp_TB_XSJRD_JY_GetModel", parameters, "ds");
            if (ds.Tables[0].Rows.Count > 0)
            {
                DataRow r = ds.Tables[0].Rows[0];
                return GetModel(r);
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获取泛型数据列表
        /// </summary>
        public IList<Model.TaoBao.TB_XSJRD_JY> GetList()
        {
            SqlParameter[] parameters ={ };
            DataSet ds = SqlHelper.RunProcedure(SqlHelper.LocalSqlServer, "sp_TB_XSJRD_JY_GetList", parameters, "ds");
            return GetList(ds);
        }

        /// <summary>
        /// 分页获取泛型数据列表
        /// </summary>
        public IList<Model.TaoBao.TB_XSJRD_JY> GetList(int pageSize, int pageIndex, string fldSort, bool sort, string strCondition, out int pageCount, out int count)
        {
            string strSql;
            DataSet ds = SqlHelper.PageList(SqlHelper.LocalSqlServer, "[TB_XSJRD_JY]", null, pageSize, pageIndex, fldSort, sort, strCondition, "MID", false, out pageCount, out count, out strSql);
            return GetList(ds);
        }
        #endregion

        /// <summary>
        /// 由一行数据得到一个实体
        /// </summary>
        private Model.TaoBao.TB_XSJRD_JY GetModel(DataRow r)
        {
            Model.TaoBao.TB_XSJRD_JY model = new Model.TaoBao.TB_XSJRD_JY();
            model.MID = SqlHelper.GetInt(r["MID"]);
            model.PID = SqlHelper.GetInt(r["PID"]);
            model.TN_ID = SqlHelper.GetInt(r["TN_ID"]);
            model.alipay_no = SqlHelper.GetString(r["alipay_no"]);
            model.payment = SqlHelper.GetString(r["payment"]);
            model.status = SqlHelper.GetString(r["status"]);
            model.type = SqlHelper.GetString(r["type"]);
            model.created = SqlHelper.GetDateTime(r["created"]);
            model.seller_rate = SqlHelper.GetInt(r["seller_rate"]);
            model.buyer_rate = SqlHelper.GetInt(r["buyer_rate"]);
            model.seller_rate_content = SqlHelper.GetString(r["seller_rate_content"]);
            model.buyer_rate_content = SqlHelper.GetString(r["buyer_rate_content"]);
            model.field1 = SqlHelper.GetString(r["field1"]);
            model.field2 = SqlHelper.GetString(r["field2"]);
            return model;
        }

        /// <summary>
        /// 由数据集得到泛型数据列表
        /// </summary>
        private IList<Model.TaoBao.TB_XSJRD_JY> GetList(DataSet ds)
        {
            List<Model.TaoBao.TB_XSJRD_JY> l = new List<Model.TaoBao.TB_XSJRD_JY>();
            foreach (DataRow r in ds.Tables[0].Rows)
            {
                l.Add(GetModel(r));
            }
            return l;
        }
    }
}
